const MAP_BY_ITEM_ORDER = [
  2,
  6,
  7,
  10,
  18,
  21,
  22,
  23,
  24,
  25,
  26,
  27,
  30,
  31,
  32,
  34,
  35,
  36,
  37,
  39,
  40,
  41,
  42,
  43,
  44,
  45,
  46,
  48,
  51,
  52,
  53,
  54,
  55,
  56,
  57,
  58,
  59,
  60,
  61,
  62,
  63,
  64,
  65,
  66,
  67,
  68,
  69,
  70,
  71,
  72,
  73,
  74,
  75,
  76,
  77,
  78,
  79,
  80,
  81,
  82,
  83,
  84,
  85,
  86,
  87,
  88,
];

const MAP_BY_DIFFICULTY_ORDER = [
  83,
  82,
  87,
  86,
  88,
  81,
  74,
  80,
  76,
  75,
  77,
  73,
  58,
  71,
  79,
  57,
  78,
  85,
  84,
  72,
  70,
  69,
  60,
  61,
  64,
  63,
  56,
  62,
  51,
  68,
  46,
  35,
  55,
  45,
  59,
  54,
  66,
  32,
  65,
  37,
  31,
  42,
  43,
  44,
  40,
  48,
  52,
  36,
  67,
  53,
  41,
  39,
  30,
  26,
  27,
  34,
  25,
  24,
  23,
  7,
  18,
  6,
  2,
  10,
  21,
  22,
];

const SCORES = {
  '83': {
    '0': 72.5,
    '1': 81.47,
    '2': 86.99,
    '3': 93.94,
  },
  '82': {
    '0': 72.19,
    '1': 81.71,
    '2': 87.41,
    '3': 94.4,
  },
  '87': {
    '0': 67.12,
    '1': 74.92,
    '2': 80.27,
    '3': 86.97,
  },
  '86': {
    '0': 65.24,
    '1': 70.65,
    '2': 74.67,
    '3': 79.84,
  },
  '88': {
    '0': 64.3,
    '1': 68.67,
    '2': 71.88,
    '3': 76.01,
  },
  '81': {
    '0': 60.73,
    '1': 66.97,
    '2': 71.66,
    '3': 79.33,
  },
  '74': {
    '0': 59.76,
    '1': 69.47,
    '2': 77.1,
    '3': 86.57,
  },
  '80': {
    '0': 59.7,
    '1': 68.16,
    '2': 78.95,
    '3': 94.68,
  },
  '76': {
    '0': 59.39,
    '1': 65.13,
    '2': 69.5,
    '3': 75.53,
  },
  '75': {
    '0': 59.09,
    '1': 65.01,
    '2': 69.55,
    '3': 76.13,
  },
  '77': {
    '0': 57.01,
    '1': 63.47,
    '2': 67.82,
    '3': 73.02,
  },
  '73': {
    '0': 55.64,
    '1': 63.18,
    '2': 68.97,
    '3': 77.73,
  },
  '58': {
    '0': 55.39,
    '1': 63.94,
    '2': 81.41,
    '3': 91.6,
  },
  '71': {
    '0': 55.27,
    '1': 59.55,
    '2': 62.97,
    '3': 67.75,
  },
  '79': {
    '0': 54.91,
    '1': 58.79,
    '2': 61.81,
    '3': 65.66,
  },
  '57': {
    '0': 54.76,
    '1': 69.3,
    '2': 81.62,
    '3': 92.36,
  },
  '78': {
    '0': 54.53,
    '1': 58.18,
    '2': 61.21,
    '3': 64.8,
  },
  '85': {
    '0': 53.36,
    '1': 63.93,
    '2': 70.48,
    '3': 78.18,
  },
  '84': {
    '0': 53.14,
    '1': 60.82,
    '2': 65.62,
    '3': 71.52,
  },
  '72': {
    '0': 52.67,
    '1': 56.29,
    '2': 59.38,
    '3': 63.03,
  },
  '70': {
    '0': 52.45,
    '1': 55.87,
    '2': 58.79,
    '3': 62.51,
  },
  '69': {
    '0': 50.06,
    '1': 53.88,
    '2': 57.05,
    '3': 60.73,
  },
  '60': {
    '0': 48.25,
    '1': 56.29,
    '2': 64.4,
    '3': 76.84,
  },
  '61': {
    '0': 47.82,
    '1': 56.22,
    '2': 65.2,
    '3': 78.41,
  },
  '64': {
    '0': 47.44,
    '1': 52.68,
    '2': 56.97,
    '3': 63.35,
  },
  '63': {
    '0': 47.08,
    '1': 54.11,
    '2': 60.78,
    '3': 71.73,
  },
  '56': {
    '0': 46.3,
    '1': 52.35,
    '2': 56.79,
    '3': 62.73,
  },
  '62': {
    '0': 45.71,
    '1': 53.08,
    '2': 60.13,
    '3': 72.04,
  },
  '51': {
    '0': 43.79,
    '1': 50.47,
    '2': 55.47,
    '3': 62.5,
  },
  '68': {
    '0': 41.3,
    '1': 47.3,
    '2': 51.48,
    '3': 56.7,
  },
  '46': {
    '0': 41.39,
    '1': 47.3,
    '2': 51.48,
    '3': 56.7,
  },
  '35': {
    '0': 41.1,
    '1': 46.02,
    '2': 49.48,
    '3': 54.29,
  },
  '55': {
    '0': 40.74,
    '1': 47.99,
    '2': 53.58,
    '3': 61.79,
  },
  '45': {
    '0': 39.78,
    '1': 44.98,
    '2': 48.68,
    '3': 53.33,
  },
  '59': {
    '0': 39.47,
    '1': 49.3,
    '2': 55.88,
    '3': 63.59,
  },
  '54': {
    '0': 39.43,
    '1': 47.81,
    '2': 53.82,
    '3': 61.92,
  },
  '66': {
    '0': 37.96,
    '1': 43.66,
    '2': 47.77,
    '3': 52.99,
  },
  '32': {
    '0': 37.83,
    '1': 43.09,
    '2': 46.92,
    '3': 51.94,
  },
  '65': {
    '0': 37.79,
    '1': 43.64,
    '2': 47.72,
    '3': 53.19,
  },
  '37': {
    '0': 37.55,
    '1': 44.85,
    '2': 50.62,
    '3': 58.79,
  },
  '31': {
    '0': 37.39,
    '1': 42.63,
    '2': 46.23,
    '3': 51.04,
  },
  '42': {
    '0': 37.24,
    '1': 42.17,
    '2': 45.96,
    '3': 51.71,
  },
  '43': {
    '0': 37.25,
    '1': 42.42,
    '2': 46.45,
    '3': 52.12,
  },
  '44': {
    '0': 37.14,
    '1': 41.08,
    '2': 44.06,
    '3': 47.88,
  },
  '40': {
    '0': 36.36,
    '1': 41.24,
    '2': 44.82,
    '3': 50.08,
  },
  '48': {
    '0': 35.6,
    '1': 42.62,
    '2': 47.87,
    '3': 55.56,
  },
  '52': {
    '0': 35.2,
    '1': 41.35,
    '2': 45.48,
    '3': 50.13,
  },
  '36': {
    '0': 35.15,
    '1': 42.76,
    '2': 47.66,
    '3': 53.94,
  },
  '67': {
    '0': 33.73,
    '1': 38.9,
    '2': 42.62,
    '3': 47.49,
  },
  '53': {
    '0': 33.36,
    '1': 43.21,
    '2': 50.75,
    '3': 59.95,
  },
  '41': {
    '0': 31.7,
    '1': 37.73,
    '2': 41.64,
    '3': 46.72,
  },
  '39': {
    '0': 30.61,
    '1': 36.8,
    '2': 40.93,
    '3': 46.65,
  },
  '30': {
    '0': 28.97,
    '1': 35.66,
    '2': 40.38,
    '3': 46.95,
  },
  '26': {
    '0': 28.57,
    '1': 35.76,
    '2': 40.21,
    '3': 45.96,
  },
  '27': {
    '0': 28.34,
    '1': 35.48,
    '2': 39.77,
    '3': 44.97,
  },
  '34': {
    '0': 27.27,
    '1': 34.0,
    '2': 39.03,
    '3': 45.48,
  },
  '25': {
    '0': 22.69,
    '1': 30.57,
    '2': 36.79,
    '3': 45.52,
  },
  '24': {
    '0': 22.24,
    '1': 28.33,
    '2': 32.52,
    '3': 37.44,
  },
  '23': {
    '0': 15.5,
    '1': 20.84,
    '2': 25.14,
    '3': 30.52,
  },
  '7': {
    '0': 13.64,
    '1': 21.21,
    '2': 27.12,
    '3': 35.76,
  },
  '18': {
    '0': 12.44,
    '1': 20.55,
    '2': 27.27,
    '3': 36.97,
  },
  '6': {
    '0': 12.29,
    '1': 20.57,
    '2': 27.78,
    '3': 37.59,
  },
  '2': {
    '0': 12.11,
    '1': 20.42,
    '2': 25.92,
    '3': 32.34,
  },
  '10': {
    '0': 5.35,
    '1': 13.79,
    '2': 20.24,
    '3': 29.24,
  },
  '21': {
    '0': 4.39,
    '1': 10.7,
    '2': 15.1,
    '3': 21.03,
  },
  '22': {
    '0': 2.33,
    '1': 14.27,
    '2': 22.68,
    '3': 31.44,
  },
};

const sortData = (type, answers) => {
  // console.log(type);

  const mapping = {};
  // 先產生對應 { no:sort }
  if (type === 1) {
    MAP_BY_ITEM_ORDER.forEach((item, index) => {
      mapping[item] = index;
    });
  } else {
    MAP_BY_DIFFICULTY_ORDER.forEach((item, index) => {
      mapping[item] = index;
    });
  }

  const data = [];

  const [answer] = answers;

  const { answerQuestions } = answer;

  answerQuestions.forEach(question => {
    const { title } = JSON.parse(question.questionContent);

    // console.log(question.questionNo, ':', title);

    data.push({
      no: question.questionNo,
      title: title.split('|')[1] || title,
      answer: question.objectAnswer,
      values: SCORES[question.questionNo],
      sort: mapping[question.questionNo],
    });
  });

  return data;
};

export default {
  sortData,
};
